package com.booksys.controller;

import com.booksys.mapper.BorrowrecordMapper;
import com.booksys.mapper.SubalbumMapper;
import com.booksys.pojo.Borrowrecord;
import com.booksys.pojo.Subalbum;
import com.booksys.result.ResultUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/borrowrecord")
@Api(tags = "图书借阅记录及归还")
public class BorrowrecordController {
    @Autowired
    private BorrowrecordMapper borrowrecordMapper;

    @Autowired
    private SubalbumMapper subalbumMapper;
    /**
     *获取全部借阅记录
     * @param raccount
     * @param currentPage
     * @return
     */
    @ApiOperation(value = "书籍借阅记录")
    @RequestMapping(value = "/getAllBorrowRecords", method={RequestMethod.POST,RequestMethod.GET})
    public ResultUtil<List<Borrowrecord>> getAllBorrowRecords(@RequestParam("raccount")@ApiParam(value="用户名")String raccount,
                                          @RequestParam("currentPage")@ApiParam(value="当前页号")int currentPage){
        ResultUtil<List<Borrowrecord>> result = new ResultUtil<>();
        List<Borrowrecord> list=borrowrecordMapper.selectAllInfoByRaccount(raccount);
        return result.success(list);
    }

    @ApiOperation(value = "书籍归还管理员确认")
    @RequestMapping(value = "/reback", method={RequestMethod.POST,RequestMethod.GET})
    public ResultUtil<Borrowrecord> reback(@RequestParam("bid")@ApiParam(value="书籍ID")int bid,
                                     @RequestParam("sid")@ApiParam(value="书籍副本号")int sid){
        ResultUtil<Borrowrecord> result = new ResultUtil<>();
        if(bid!=0&&sid!=0){
            borrowrecordMapper.delete(borrowrecordMapper.selectByPrimaryKey(bid));
            Subalbum subalbum=subalbumMapper.selectByPrimaryKey(sid);
            subalbum.setCondi(0);
            subalbumMapper.updateByPrimaryKey(subalbum);
            return result.success();
        }else{
            return result.fail();
        }
    }
}
